/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backingbeans;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import sessions.UserManager;
import utils.ClientMessage;
import utils.FormValidation;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@Named(value = "register")
@RequestScoped
public class Register {

    @EJB
    private UserManager userManager;
    private String avatar = ""; // TODO url avatar par défaut
    private String email;
    private String firstname;
    private String lastname;
    private String password;
    private String c_email;
    private String c_password;

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getC_email() {
        return c_email;
    }

    public void setC_email(String c_email) {
        this.c_email = c_email;
    }

    public String getC_password() {
        return c_password;
    }

    public void setC_password(String c_password) {
        this.c_password = c_password;
    }

    public void register() {
        
        if(userManager.existEmail(email)==true)
        {
            //If the given email is already used for an existing user
            //Show an error message
            //
            ClientMessage.sendClientMessage("Cette adresse email est déjà utilisée", FacesMessage.SEVERITY_ERROR);
        }
        else
        {        
            //Check email
            //
            if(FormValidation.validateEmailRegex(email)==false)
            {
                ClientMessage.sendClientMessage("L'adresse email que vous avez entrée est invalide", FacesMessage.SEVERITY_ERROR);
                return;
            }
            
            if(email.compareTo(c_email)!=0)
            {
                ClientMessage.sendClientMessage("La confirmation de l'adresse email ne correspond pas", FacesMessage.SEVERITY_ERROR);
                return;
            }
            
            //Check password
            //
            if(password.length() < 6)
            {
                ClientMessage.sendClientMessage("La longueur du mot de passe doit être d'au moins 6 caractères", FacesMessage.SEVERITY_ERROR);
                return;
            }
            
            if(password.compareTo(c_password)!=0)
            {
                ClientMessage.sendClientMessage("La confirmation du mot de passe ne correspond pas", FacesMessage.SEVERITY_ERROR);
                return;
            }
            
            //Create the new user
            //
            userManager.createUser(avatar, email, firstname, lastname, password);
            
            //Show a success message
            //
            ClientMessage.sendClientMessage("Félicitations ! Vous êtes maintenant inscrit !", FacesMessage.SEVERITY_INFO);
        }
    }

}
